﻿package sedie 
{
	import common.Utility;
	
	import flash.events.Event;
	import flash.net.Responder;
	import flash.utils.setTimeout;
	
	import game.ChannelControl;
	import game.GameBase;
	import game.GlobalVariables;
	import game.SoundController;
	import game.VolumeController;
	import game.constants.ConstData;
	import game.events.EventData;
	
	import sedie.exchangeddata.BetInfo;
	import sedie.exchangeddata.StageInfo;
	
	import test.views.ViewAllTest;
	
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class SedieGame extends GameBase
	{
		public var gameInfo:StageInfo;
		private var channelControl:ChannelControl;
		public var snd:SoundController;		
		public var volumnGame:VolumeController;
		public function SedieGame() 
		{
			this.gameInfo = new StageInfo();
			this.snd = new SoundController();	
			this.channelControl = new ChannelControl();
			GlobalVariables.gameRuleTxt = "rulesedie.txt";			
			/**
			 *  Run Flash	
			 */ 
			GlobalVariables.roomId = "5";
			GlobalVariables.rtmpStr = "10.10.100.203/NONSQL";//"localhost/Jewel2";//"10.10.100.203/NONSQL"//"113.21.208.2/Jewel4Beta";
			this.channelControl.initFmsMonitor(GlobalVariables.fmsMonitor);
			this.channelControl.addEventListener(EventData.SET_FMS_CHANNEL, updateVideoHdl);
							
			super.stageInfo.userInfo.init(GlobalVariables.userName, GlobalVariables.preUser, GlobalVariables.isMain, GlobalVariables.roomId);
			
			this.connectToNc(GlobalVariables.rtmpStr, "0", GlobalVariables.userType, super.stageInfo.userInfo.preUser + super.stageInfo.userInfo.userName, GlobalVariables.pass, 
					GlobalVariables.pBetLtdSeq, GlobalVariables.sessionId, GlobalVariables.lang, GlobalVariables.versionNo, 
					GlobalVariables.forceReload, this.stageInfo.userInfo.isMain, GlobalVariables.siteName, GlobalVariables.casinoId, 
					GlobalVariables.ncName, GlobalVariables.ip, GlobalVariables.countryCode, GlobalVariables.loginTypeID, GlobalVariables.parentSID);
			
			/* RUN TEST */
			new ViewAllTest();
			/**
			 *  End Run Flash	
			 */ 	
			this.addEventListener(EventData.CONNECT_GAME, connectGame); 
			super.stageInfo.welcomeMessage.addEventListener(EventData.UPDATE_WELCOME_MESSAGE, updateWelcomeMessage);
			super.stageInfo.tableInfo.addEventListener(EventData.UPDATE_TABLEINFO, updateTableInfo);
			super.stageInfo.tableInfo.addEventListener(EventData.UPDATE_DATECLOCK, updateDateClock);
			//history
			this.gameInfo.historyInfo.addEventListener(EventData.UPDATE_HISTORY, updateGameHis);
			//livebetpool
			this.gameInfo.userLiveBetPoolInfo.addEventListener(EventData.UPDATE_LIVEBETPOOL_USER_PUBLIC, updateLiveBetPoolUserPublic);
			super.stageInfo.userInfo.addEventListener(EventData.UPDATE_USERINFO, updateUserInfo);
			super.stageInfo.userInfo.addEventListener(EventData.UPDATE_CURRENCY, updateCurrency);
			super.stageInfo.userInfo.addEventListener(EventData.UPDATE_BALANCE, updateBalance);
			this.addEventListener(EventData.COUNTDOWN_COMPLETE, countDownCompleteHandler);
			this.addEventListener(EventData.CONFIRM_BETTING, betControllerHandler);
			this.addEventListener(EventData.CLEAR_BETTING, betControllerHandler);
			this.addEventListener(EventData.REBET_BETTING, betControllerHandler);
			this.addEventListener(EventData.GET_SOUND_COUNT_DOWN, hdlGetSoundCountDown);
			this.addEventListener(EventData.UPDATE_VIDEO, hdlUpdateFmsChannel);
			this.addEventListener(EventData.UPDATE_SOUNDVOLUMEBACKGROUND, hdlUpdateSoundVolumnBackground);
			this.addEventListener(EventData.UPDATE_SOUNDBACKGROUND, hdlUpdateSoundBackground);
			this.addEventListener(EventData.STOP_LIST_SOUND, stopSoundHdl);
			this.addEventListener(EventData.LOCK_VOLUME_GAME, lockVolumeGameHdl);
			this.addEventListener(EventData.UN_LOCK_VOLUME_GAME, unlockVolumeGameHdl);
			this.addEventListener(EventData.GET_RECENT_BET, getRecentBetHdl); 
			
		}
		private function updateCurrency(e:Event):void {			
			super.currentView.initCurrency(GlobalVariables.currency);
		}
		private function getRecentBetHdl(e:EventData):void 
		{
			getRecentBet(uint(e.ms), 20);
		}
		
		private function unlockVolumeGameHdl(e:EventData):void 
		{
			super.unClockVolumeGame();
			//trace("unlockVolumeGameHdl");
		}
		
		private function lockVolumeGameHdl(e:EventData):void 
		{
			//trace("lockVolumeGameHdl");
			super.lockVolumeGame();
		}
		
		private function stopSoundHdl(e:EventData):void 
		{
			this.snd.stopSound();
		}
		
		private function hdlUpdateSoundVolumnBackground(e:EventData):void {			
			var temp:Number = Number(e.newBalance);
			snd.setVolumnSound(temp);
			GlobalVariables.volumeSoundBackground = temp;
			trace("hdlUpdateSoundVolumnBackground" + temp);
		}
		
		private function hdlUpdateSoundBackground(e:EventData):void {
			
			snd.playMusic(GlobalVariables.soundActive);
			//trace("hdlUpdateSoundBackground");
		}
		
		private function hdlUpdateFmsChannel(e:EventData):void 
		{
			this.channelControl.fmsChannel = e.ms;
			//this.updateVideoPath(GlobalVariables.fmsChannel);
			updateVideoPath(channelControl.fmsChannel);
		}
		private function updateVideoHdl(e:EventData):void 
		{
			//trace("vo updateVideoHdl");
			activeVideo(this.channelControl);
			
		}
		
		public function activeVideo(channel:ChannelControl):void 
		{
			this.channelControl = channel;
			super.currentView.initListChannel(this.channelControl.channelInfo.listChannel, this.channelControl.fmsChannel);
			//this.updateVideoPath(GlobalVariables.fmsChannel);
			updateVideoPath(channelControl.fmsChannel);
		}
		
		private function updateDateClock(e:EventData):void 
		{
			super.currentView.updateDateClock(super.stageInfo.tableInfo);		
		}
		
		private function hdlGetSoundCountDown(e:EventData):void 
		{
			super.sndCtr.playSound(GlobalVariables.dirSnd + "bip.mp3", GlobalVariables.volumeSounGame);
			
		}
		
		private function betControllerHandler(e:EventData):void 
		{
			if(e.type == EventData.CONFIRM_BETTING)
				dobet();
			else if (e.type == EventData.CLEAR_BETTING)
				this.currentView.clearBetting(e);	
			else if (e.type == EventData.REBET_BETTING)
				this.currentView.rebetBetting(e);
		}
	
		private function updateUserInfo(e:EventData):void 
		{
			//trace("updateUserInfo");
			super.setMinMaxTableInfo(super.stageInfo.userInfo.minBet, super.stageInfo.userInfo.maxBet);					
			// Update Chip Betting
			currentView.updateChipBetting(super.stageInfo.userInfo.minBet, super.stageInfo.userInfo.maxBet, 
										super.stageInfo.userInfo.balance, super.stageInfo.userInfo.chip);
			currentView.updateTableMinMax(super.stageInfo.tableInfo.minBet, super.stageInfo.tableInfo.maxBet);
			currentView.updateUserName(super.stageInfo.userInfo.userName);
			// Call currUserBet to get the value betting of user
			callGetCurrUserBet();
			/*MovieClip(parent.parent.parent).initUserInfo(GlobalVariables.userName);	*/	
		}
		
		private function updateLiveBetPoolUserPublic(e:Event):void {
			super.currentView.updateLiveBetPoolUserPublic(this.gameInfo.userLiveBetPoolInfo);
		}
		
		private function updateGameHis(e:EventData):void 
		{
			//trace("updateGameHis");
			super.currentView.updateGameHis(this.gameInfo.historyInfo);		
		}
		
		private function connectGame(e:Event):void {
			super.connectToNcId(GlobalVariables.rtmpStr, super.stageInfo.userInfo.roomId, GlobalVariables.userType, 
								super.stageInfo.userInfo.preUser + super.stageInfo.userInfo.userName, 
								super.stageInfo.userInfo.sessionId, GlobalVariables.memberBetLtdSeq);		
		}
		
		private function updateWelcomeMessage(e:Event):void {			
			 super.currentView.updateWelcomeMessage(super.stageInfo.welcomeMessage.strMsg);
		}
		
		private function updateTableInfo(e:Event):void {				
			super.currentView.updateTableInfo(super.stageInfo.tableInfo);				
			if (super.stageInfo.tableInfo.status.toUpperCase() == "STOP") {				
				currentView.initStopGame();
			}				
		}	
	
		override public function setBetStatus(result:String, tableNo:String):void 
		{
			super.setBetStatus(result, tableNo);
			if (Utility.checkResult(result)) {
				
				//trace("update his" + result.substr(result.length - 18, result.length));
				this.gameInfo.historyInfo.setHistory(this.gameInfo.historyInfo.returnSetHistory(result));
			}
				
		}
		
		override public function currentBetPool(result:String):void 
		{
			if (Utility.checkResult(result)) {
				//trace("currentBetPool:" + result);
				this.gameInfo.userLiveBetPoolInfo.returnCurrentBetPool(result);
			}
		}
		
		
		private function updateBalance(e:Event):void {					
			currentView.updateBalance(super.stageInfo.userInfo.balance);
		}
		override public function setMemberLimit(result:String):void 
		{
			//super.setMemberLimit(result);
			//trace("setMemberLimitSic" + result);
			if(Utility.checkResult(result)){
				stageInfo.userInfo.resultSetMemberLimit(result);				
			}
		}
		
		private function countDownCompleteHandler(e:Event):void {
			if(GlobalVariables.autoAcceptBetting == "1"){
				dobet();
			}
			this.currentView.initStopGame();			
		}
		private function dobet():void {
			var confirmBetting:String = this.currentView.confirmBetting();			
			var arrBetting:Array = new Array();
			if (confirmBetting != "") {				
				arrBetting = confirmBetting.split("@");	
				trace("arrBetting[0]:" + arrBetting[0] + ":" + arrBetting[1]);
				super.ncId.call("placeBet_SD", new Responder(super.successCallFMS, super.failCallFMS), stageInfo.userInfo.sessionId, GlobalVariables.memberBetLtdSeq,
				super.stageInfo.tableInfo.gameSet, super.stageInfo.tableInfo.gameNo, arrBetting[0], arrBetting[1]);
			}
		}
		
		override public function setPlaceBet(result:String):void {
			var ms:String;
			trace("setPlaceBet:" + result);
			if (Utility.checkResult(result)) {			
				this.currentView.saveBettingSuccess();				
				
				//Show Bet Success
				this.currentView.showBetSuccess(true, true);
				
				ms = GlobalVariables.langInfo.hashTable.lookupString("BSuccess");
			} else {
				this.currentView.updateBetting();
				ms = GlobalVariables.langInfo.hashTable.lookupString(result.substr(1, result.length));
			}
			super.setTimelyMessage(ms);	
		}
		
		override public function setPlaceBet1(result:String):void {			
				if(Utility.checkResult(result)){
				var currBetInfo:BetInfo = new BetInfo();
				var dis:uint = 4
				var pos:uint = 5;
				var len:uint = 9;
				trace("setPlaceBet1:" + result);
				for (var i:int = 0; i < currBetInfo.arrBettingCode.length; i++) {
					if(parseFloat(result.substr(pos, len)) != 0) 
					{
						currBetInfo.arrBettingValue[i] = Number(result.substr(pos, len));	
					}
					pos += len + dis;
				}
				this.currentView.updateCurrUserBet(currBetInfo);
			}
		}
		
		
		
		override public function showGameResult(result:String):void 
		{
			super.showGameResult(result);
			trace("showGameResult" + result);
			if (Utility.checkResult(result)) {
				
				super.currentView.showWinLose(result);
				playSoundResultGame(result);
			}
		}
		
		private function playSoundResultGame(result:String):void 
		{
			var temp:String = result.substr(ConstData.POS_SEDIE_RESULT, ConstData.LEN_SEDIE_RESULT);
			
			switch(temp) {
				
				case "0":
					super.sndCtr.playSound(GlobalVariables.dirSnd + "4_White_Event.mp3", GlobalVariables.volumeSounGame);
					break;
				case "1":
					super.sndCtr.playSound(GlobalVariables.dirSnd + "3_White_Odd.mp3", GlobalVariables.volumeSounGame);
					break;
				case "2":
					super.sndCtr.playSound(GlobalVariables.dirSnd + "2_Pairs_Even.mp3", GlobalVariables.volumeSounGame);
					break;
				case "3":
					super.sndCtr.playSound(GlobalVariables.dirSnd + "3_Red_Odd.mp3", GlobalVariables.volumeSounGame);
					break;
				case "4":
					super.sndCtr.playSound(GlobalVariables.dirSnd + "4_Red_Event.mp3", GlobalVariables.volumeSounGame);
					break;
				
			}	
			
		}
		override public function setCardResultDelay(cardCode:String, where:String, instanceName:String):void 
		{
			//trace("setCardResultDelay" + "cardCode:" + cardCode +" where: "+ where +" instanceName :"+  instanceName);
			super.currentView.updateGameCard(where, cardCode);
		}
		
		override public  function confirmedConnection(str:String):void {	
    		super.confirmedConnection(str);		
	        //init interface
			super.currentView.initGame();
			//get data
			if (GlobalVariables.fmsChannel != "")
			{
				//trace("fmsChannel ");
				//updateVideoPath(GlobalVariables.fmsChannel);
				updateVideoPath(channelControl.fmsChannel);
			}
			super.ncId.call("getServerDate2", new  Responder(successCallFMS, failCallFMS), stageInfo.userInfo.sessionId);
			
		}	
		
		override public function setServerDate2(result:String):void 
		{
			super.setServerDate2(result);
			super.stageInfo.tableInfo.returnDateClock(result);
		}
		
		override public function showResult(result:String, tableNo:String):void 
		{	
			//trace("showResult");
			super.showResult(result, tableNo);
			if (Utility.checkResult(result)) {	
				if (this.gameInfo.historyInfo.odlResult == result)
					return;				
				this.gameInfo.historyInfo.setHistory(this.gameInfo.historyInfo.returnSetHistory(result));		
				
				
			}
		}
		
		override public function sendStatusGame(InstanceName:String, gameType:String, eventType:String):void {		   
		    if (eventType == "NewGameset" && InstanceName == super.stageInfo.userInfo.roomId){				
				this.gameInfo.historyInfo.setHistory(this.gameInfo.historyInfo.returnSetHistory("0")); 
		   }
		}
		override public function updateVideoPath(fmsVideo:String):void 
		{
			super.updateVideoPath(fmsVideo);
		}
		/*public function updateVideoPath(fmsVideo:String) {
			
			var temp:String;
			switch(super.stageInfo.userInfo.roomId) {
				case "6":
				case "20":
					temp = channelControl.fmsChannel + "@" + "grd-t03-live1";
					break;
				case "21":
					temp = channelControl.fmsChannel + "@" + "gmg-t01-live2";
					break;	
				case "22":
					temp = channelControl.fmsChannel + "@" + "gmg-l01-live2";
					break;
				case "23":
					temp = channelControl.fmsChannel + "@" +  "gmg-t02-live2";
					break;
				case "24":
					temp = channelControl.fmsChannel + "@" +  "grd-t04-live1";
					break;
				case "25":
					temp = channelControl.fmsChannel + "@" + "grd-f01-live1";
					break;
				
				case "26":
					temp = channelControl.fmsChannel + "@" +  "gmg-f01-live2";
					break;	
				case "27":
					temp = channelControl.fmsChannel + "@" +  "grd-l01-live2";
					break;
				case "28":
					temp = channelControl.fmsChannel + "@" +  "grd-s01-live1";
					break;
				case "29":
					temp = channelControl.fmsChannel + "@" +  "bav-r01-live1";
					break;
				default:
					temp = channelControl.fmsChannel + "@" + "grd-t03-live1";
					break;
			}
			
			GlobalVariables.fmsChannel = temp;
			trace("GlobalVariables.fmsChannel:" + GlobalVariables.fmsChannel);
			//trace("GlobalVariables.fmsChannel:" + GlobalVariables.fmsChannel + "temp:" + temp + "super.stageInfo.userInfo.roomId" + super.stageInfo.userInfo.roomId);
			super.currentView.updateVideoPath(GlobalVariables.fmsChannel);			
		}*/
		
		public function rightClick():void {							
			this.currentView.rightBetting();
		}
		
	}

}